/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package sonia.golf.dao.sqlite;

//~--- non-JDK imports --------------------------------------------------------

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import sonia.golf.Player;
import sonia.golf.dao.DAOException;
import sonia.golf.dao.DAOListener.DAOEvent;
import sonia.golf.dao.PlayerDAO;
import sonia.golf.util.AssertUtil;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Sebastian Sdorra
 */
public class SQLitePlayerDAO extends AbstractSQLiteDAO<Player, String>
        implements PlayerDAO
{

  /** Field description */
  public static final String QUERY_COUNT = "player.count";

  /** Field description */
  public static final String QUERY_INSERT = "player.insert";

  /** Field description */
  public static final String QUERY_REMOVE = "player.remove";

  /** Field description */
  public static final String QUERY_SELECT = "player.select";

  /** Field description */
  public static final String QUERY_SELECT_ALL = "player.select-all";

  //~--- constructors ---------------------------------------------------------

  /**
   * Constructs ...
   *
   *
   * @param helper
   */
  SQLitePlayerDAO(SQLiteHelper helper)
  {
    this.helper = helper;
  }

  //~--- methods --------------------------------------------------------------

  /**
   * Method description
   *
   *
   * @param item
   */
  public void add(Player item)
  {
    SQLiteDatabase db = helper.getWritableDatabase();

    db.beginTransaction();

    try
    {
      String query = helper.getQuery(QUERY_INSERT);

      AssertUtil.assertIsNotEmpty(query);

      SQLiteStatement statement = db.compileStatement(query);

      statement.bindString(1, item.getName());
      statement.bindLong(2, item.getCreationDate().getTime());
      statement.executeInsert();
      db.setTransactionSuccessful();
      fireEvent(item, DAOEvent.ADD);
    }
    finally
    {
      db.endTransaction();
    }
  }

  /**
   * Method description
   *
   *
   * @return
   */
  public long count()
  {
    long count = -1;
    Cursor c = helper.executeQuery(QUERY_COUNT);

    try
    {
      if (c.moveToNext())
      {
        count = c.getLong(0);
      }
    }
    finally
    {
      c.close();
    }

    return count;
  }

  /**
   * Method description
   *
   * TODO: check for played games
   *
   * @param item
   */
  public void remove(Player item)
  {
    SQLiteDatabase db = helper.getWritableDatabase();

    db.beginTransaction();

    try
    {
      String query = helper.getQuery(QUERY_REMOVE);

      AssertUtil.assertIsNotEmpty(query);

      SQLiteStatement statement = db.compileStatement(query);

      statement.bindString(1, item.getName());
      statement.execute();
      db.setTransactionSuccessful();
      fireEvent(item, DAOEvent.REMOVE);
    }
    finally
    {
      db.endTransaction();
    }
  }

  /**
   * Method description
   *
   *
   * @param item
   */
  public void update(Player item)
  {
    throw new DAOException("cannot update an player");
  }

  //~--- get methods ----------------------------------------------------------

  /**
   * Method description
   *
   *
   * @param name
   *
   * @return
   */
  public Player get(String name)
  {
    Cursor c = helper.executeQuery(QUERY_SELECT, name);
    Player player = null;

    try
    {
      if (c.moveToNext())
      {
        player = new Player(name, new Date(c.getLong(0)));
      }
    }
    finally
    {
      c.close();
    }

    return player;
  }

  /**
   * Method description
   *
   *
   * @return
   */
  public List<Player> getAll()
  {
    Cursor c = helper.executeQuery(QUERY_SELECT_ALL);
    List<Player> players = new ArrayList<Player>();

    try
    {
      while (c.moveToNext())
      {
        players.add(new Player(c.getString(0), new Date(c.getLong(1))));
      }
    }
    finally
    {
      c.close();
    }

    return players;
  }
  //~--- fields ---------------------------------------------------------------

  /** Field description */
  private SQLiteHelper helper;
}
